home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1230 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: ix.netcom.com!netnews
  2. From: miker3@ix.netcom.com (Mike Rubenstein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: returning a string from a function
  5. Date: Fri, 12 Jan 1996 12:13:53 GMT
  6. Organization: Netcom
  7. Message-ID: <30f64e6a.41247232@nntp.ix.netcom.com>
  8. References: <4d4uh8$q46@mailhost.mwmicro.com>
  9. NNTP-Posting-Host: ix-dc11-03.ix.netcom.com
  10. X-NETCOM-Date: Fri Jan 12  4:13:44 AM PST 1996
  11. X-Newsreader: Forte Agent .99c/16.141
  12.  
  13. aschlies@citynet.net (Tony Schliesser) wrote:
  14.  
  15. |>I am learning C with the aid of only a book.  I am at an inpass in
  16. |>this process.  I have a small function that needs to return a string
  17. |>back to the main routine. I have the following prototype:
  18. |>
  19. |>char function_name(char in_string[80])
  20. |>
  21. |>{
  22. |>    char value[80];
  23. |>
  24. |>    ...value takes on part of the value of the last 10 characters
  25. |>      of in_string.
  26. |>
  27. |>    return(value);
  28. |>}
  29. |>
  30. |>
  31. |>jWhen this function is done, it only returns the first character.  I
  32. |>"watched" the program execution and it shows that value indeed has
  33. the
  34. |>last 10 characters.  Any clues as to why the calling routine only
  35. gets
  36. |>the one character??
  37. |>
  38. |>Also, I wrote the routine that copies the last 10 characters. Is
  39. there
  40. |>a lib. function that does that for me?? Did I reinvent the wheel?
  41.  
  42. A few problems.
  43.  
  44. First, you've defined the function as returning a single char.  If you
  45. want to return a string, you must define it as returning a pointer to
  46. char.
  47.  
  48. Since you must return a pointer, the thing it points to must live
  49. after the function exits.  An auto variable like value in you function
  50. is deallocated when it exits.  
  51.  
  52. Either 
  53.  
  54.     1. Make value static (in which case users of the function must
  55.  
  56.        realize that the returned value becomes invalid the next
  57.        time the function is called).
  58.  
  59.     2. allocate it with malloc() (or such) (in which case the
  60.        caller must realize that it must be freed).
  61.  
  62.     3. return a pointer into the argument (in which case the 
  63.        caller must realize that the return is valid only as long 
  64.        as the argument string is unchanged.
  65.  
  66. Something like
  67.  
  68.     char* function_name(char in_string[80])
  69.     {
  70.       static char value[80];
  71.  
  72.       /* do your thing */
  73.  
  74.       return(value);
  75.     }
  76.  
  77. You don't show code for your copy, but it sounds like you could use
  78. strcpy() or strncpy().
  79.  
  80. You should be aware that in_string is really a pointer to char.
  81. Nothing in the language constrains it to being only 80 characters
  82. long, so make sure your function doesn't mess up with longer strings.
  83.  
  84.  
  85. Michael M Rubenstein
  86.